// 主仓库
import {deepCopy, objecGetValue, isEmptyObj} from '@/utils/tools'
import {  powerStatus } from '@/components/ivew_conf/power/index'
import powerFilter from '@/components/ivew_conf/power/power_filter'
import carStatusConf from '@/components/ivew_conf/car_status_conf'
import topBarNavConf from '@/components/ivew_conf/top_bar_nav_conf'

import * as personalForm from '@/components/ivew_conf/organization/personal_form'


interface state {
    userBase:userDetails.userBase | {}
    ivewOn:string
    powerConf:commonInf.powerStatus
}


class mainStore{

    state:state = {

        // 用户信息
        userBase: {},

        // 当前显示视图层( 视图名称 )
        ivewOn: '',

        // 权限配置
        powerConf: new powerStatus()
        
    }
   
    mutations = {

        // 更新用户数据
        updateUserBase (state:state, data){
            state['userBase']  = deepCopy(data);
        },

        // 切换用户显示/隐藏
        userCenterSwitch (state:state, data?:boolean){
            let type:string = '';
            let currentValue = state['ivewOn'] === 'userCenter';
            
            if(data !== undefined){
                type = data ? 'userCenter' : '';
            }else{
                type = currentValue ? '' : 'userCenter'
            } 

            state['ivewOn'] = type;
        }

    }
    
    getters = {

        // 窗口高度
        winH(state){
            return document.body.clientHeight; 
        },
        

        // 用户中心是否为显示状态
        showUserCenter(state:state):boolean{
            return state.ivewOn === 'userCenter'
        },
        
        // 返回用户中心所需数据
        userCenterInfo(state:state) {
            
            class userInfo{
                company:string = ''
                department:string = ''
                level: string = ''
                phone: string = ''
                nickname: string =''
            }
            
            let userBase = state.userBase;
            let info = new userInfo();
            
            if(!isEmptyObj(userBase)){
                info = objecGetValue<userInfo>(userBase, info);
                info.level = (<userDetails.userBase>userBase).extensions.member.level;
            }

            return info;
        }
        
        
    }

    actions = {


         // 设置权限配置(保证在获取到登录用户权限信息后, 配置整个应用权限设置)
         setPowerConf({state, commit, dispatch}, powerConf){
             
            state.powerConf = powerConf;
            
            // 车险表单按键配置
            let carFormConf = powerFilter(carStatusConf, powerConf, 'carFormBtns')
            commit('carFormStore/setCarStatusConf', carFormConf, {root:true})
            

            // 车险列表工具条
            let carTableTools = powerFilter({}, powerConf, 'carTableTools')
            commit('carTableStore/setToolsFunConf', carTableTools, {root:true})            
          
            
            // 权限组件加载配置（限制人员表单， 可配置权限项）
            let personalFormConf = powerFilter(personalForm, powerConf, 'personalForm')
           dispatch('modConf/update', {target:'personalForm', data:personalFormConf}, {root:true})


            // 导航
            let topBarNav = powerFilter(topBarNavConf, powerConf, 'nav')
           dispatch('modConf/update', {target:'topBarNavConf', data:topBarNav}, {root:true})
            
        },

        

    }
    
}



export default mainStore;